﻿@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Retrieve Submission Page";

    bool hasError = false;
    string errorMessage = string.Empty;
    bool hasSuccess = false;
    string successMessage = string.Empty;

    string frm_claimDate = string.Empty;
    string frm_claimStartWorkTime = string.Empty;
    string frm_claimEndWorkTime = string.Empty;
    string frm_claimStartOT = string.Empty;
    string frm_claimEndOT = string.Empty;
    string frm_claimHours_OT1_5 = string.Empty;
    string frm_claimHours_OT2_0 = string.Empty;
    string frm_claimHours_PH = string.Empty;
    string frm_claimHours_Less4H = string.Empty;
    string frm_claimHours_More4H = string.Empty;
    string frm_claimAllowance_Meal = string.Empty;
    string frm_claimAllowance_Shift = string.Empty;
    string frm_claimShiftType = string.Empty;
    string frm_claimRemark = string.Empty;
    string frm_claimTotalOTHours = "N/A";
    string frm_claimBreakHours = string.Empty;

    
    List<KeppelLand.Entities.Claim> claimList = null;
    KeppelLand.Entities.ClaimSummary claimSummary = null;
    System.Data.DataSet ds = new System.Data.DataSet("CurrentClaims");

    if (Session["successMessage"] != null)
    {
        hasSuccess = true;
        successMessage = Session["successMessage"].ToString().Trim();
        Session["successMessage"] = null;
    }
    

    if (IsPost)
    {
        if (Request.Form["actionButton"] == "retrieve")
        {
            string frm_actionId = Request.Form["actionId"].Trim();

            if (!string.IsNullOrEmpty(frm_actionId))
            {
                string id = KeppelLand.Helpers.CryptographyHelper.Decrypt(frm_actionId);

                if (!string.IsNullOrEmpty(id))
                {
                    if (KeppelLand.Controllers.ClaimController.RetrieveClaim(KeppelLand.Helpers.UserIdentityHelper.GetClaimantName(), id))
                    {
                        hasSuccess = true;
                        successMessage = "Claim entry retrieved.";
                    }
                }
            }
        } // if (Request.Form["actionButton"] == "remove")

        if (Request.Form["actionButton"] == "download")
        {
            byte[] fileBytes = KeppelLand.Controllers.ReportController.GetClaimsPendingSubmissionReport(
                Request.Form["download_format"], 
                KeppelLand.Helpers.UserIdentityHelper.GetClaimantName(), 
                KeppelLand.Entities.ClaimStatus.DRAFT);
            Response.Clear();
            Response.ContentType = KeppelLand.Helpers.TranslationHelper.GetContentType(Request.Form["download_format"]);
            Response.AddHeader("content-disposition", "attachment; filename=" + KeppelLand.Helpers.TranslationHelper.GetFileName(Request.Form["download_format"], "Claims"));
            Response.BinaryWrite(fileBytes);
            Response.Flush();
            Response.End();
        } // if (Request.Form["actionButton"] == "download")

    } // if (IsPost)
        
    // Other normal get operations
    claimList = KeppelLand.Controllers.ClaimController.GetRetrievableClaimList(KeppelLand.Helpers.UserIdentityHelper.GetClaimantName());
    claimSummary = KeppelLand.Controllers.ClaimController.GetRetrievableClaimSummary(KeppelLand.Helpers.UserIdentityHelper.GetClaimantName());
}

<div class="container-fluid main-content" role="main">

    @RenderPage("~/Shared/Help_Tab.cshtml")

    @if (hasError)
    {
        <div class="alert alert-danger alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            @Html.Raw(errorMessage)
        </div>
    }
    @if (hasSuccess)
    {
        <div class="alert alert-success alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            @successMessage
        </div>
    }

    @RenderPage("~/Shared/Retrievable_Claims.cshtml", claimList, claimSummary)

    <p>
        @*<button type="button" class="btn btn-primary btn-download" onclick="download_blanksheet('Excel');"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Download Manual Claim Form</button>*@
        
        @*<div class="btn-group">
            
            <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                <span class="caret"></span>
                <span class="sr-only">Toggle Dropdown</span>
            </button>
            <ul class="dropdown-menu" role="menu">
                <li onclick="download('Excel');"><a href="javascript:void(0);" class="btn-download">Download as Excel</a></li>
                <li onclick="download('WORD');"><a href="javascript:void(0);" class="btn-download">Download as Word</a></li>
                <li onclick="download('IMAGE');"><a href="javascript:void(0);" class="btn-download">Download as TIFF</a></li>
            </ul>
        </div>*@

    </p>
    
    <p>&nbsp;</p>

</div>

@RenderPage("~/Shared/Standard_Modals.cshtml")

<div class="modal fade" id="messageModal" tabindex="-1" role="dialog" aria-labelledby="messageModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="messageModalLabel">Message</h4>
            </div>
            <div class="modal-body" id="messageModalBody">
                <p>
                    <strong>Rejection Reason:</strong>
                </p>
                <p id="messageModal_rejected_reason">
                    Click OK to proceed with this action.<br />
                    Click Canel to abort this action.
                </p>

                <p>
                    <strong>Rejected By</strong><br />
                    <span id="messageModal_rejected_by"></span>
                </p>
                <p>
                    <strong>Rejected Date</strong><br />
                    <span id="messageModal_rejected_date"></span>
                </p>
            </div>

            <div class="modal-footer">

                <button type="button" class="btn btn-primary" id="messageModal_OK" data-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="retrieveConfirmationModal" tabindex="-1" role="dialog" aria-labelledby="retrieveConfirmationModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="retrieveConfirmationModalLabel">Confirm retrieve claim entry?</h4>
            </div>
            <div class="modal-body">
                <p>
                    Click OK to proceed with this action.<br />
                    Click Canel to abort this action.
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="retrieveConfirmationModal_OK">OK</button>
                <button type="button" class="btn btn-primary" id="retrieveConfirmationModal_Cancel" data-dismiss="modal">Cancel</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="helpModal" tabindex="-1" role="dialog" aria-labelledby="helpModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="helpModalLabel">Help <small>(Add New Claim)</small></h4>
            </div>
            <div class="modal-body">
                <p>
                    Use this page to submit, add new or edit existing claims.<br />
                    To add claims, follow the below instructions.
                </p>

                <div class="panel panel-default">
                    <div class="panel-heading">Instructions to add new claim</div>
                    <div class="panel-body">
                        <ol>
                            <li>Enter claim date, official hours and overtime hours. These are required fields.</li>
                            <li>
                                Fill in overtime hours if applicable.<br />
                                This is required if you intend to claim OT or meal allowances.
                            </li>
                        </ol>
                    </div>
                </div>

                <p>To edit claims, follow the below instructions.</p>

                <div class="panel panel-default">
                    <div class="panel-heading">Instructions to edit existings claim(s)</div>
                    <div class="panel-body">

                        <ol>
                            <li>Claimant to enter claim date, official hours and overtime hours.</li>
                            <li>Claimant then fill in the overtime type.</li>
                        </ol>
                    </div>
                </div>

                <p>To submit claims, follow the below instructions.</p>

                <div class="panel panel-default">
                    <div class="panel-heading">Instructions to submit claims to supervisor</div>
                    <div class="panel-body">

                        <ol>
                            <li>Claimant to enter claim date, official hours and overtime hours.</li>
                            <li>Claimant then fill in the overtime type.</li>
                        </ol>
                    </div>
                </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="helpModal_Close" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

@section Scripts
{
    <script type="text/javascript" src="~/Scripts/kendo/2014.3.1119/kendo.ui.core.min.js"></script>
    <script type="text/javascript" src="~/Scripts/moment.js"></script>
    <script type="text/javascript" src="~/Scripts/moment-with-locales.js"></script>
    <script type="text/javascript" src="~/Scripts/app/claim_entry.js"></script>
    <script type="text/javascript">

        function download(format) {
            $("#approval_form #actionButton").val("download");
            $("#approval_form #download_format").val(format);
            $("#approval_form").submit();
        }

        function ShowOtRateDialog()
        {
            $('#otRateModal').modal('show');
        }

        function ShowMessageDialog(id) {
            // Some AJAX  to fetch message
            $("#messageModal_rejected_reason").text("Retrieving data...");
            $("#messageModal_rejected_by").text("Retrieving data...");
            $("#messageModal_rejected_date").text("Retrieving data...");
            var postJson = {
                id: id
            };
            $.ajax({
                type: "POST",
                url: "TestService.svc/DoWork",
                data: JSON.stringify(postJson),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function (xhr, settings) {
                    $("#messageModal_rejected_reason").text("Retrieving details...");
                    $("#messageModal_rejected_by").text("Retrieving details...");
                    $("#messageModal_rejected_date").text("Retrieving details...");
                    $('#messageModal').modal('show');
                },
                success: function (jsonData, status, xhr) {
                    detail = jsonData.d;
                    $("#messageModal_rejected_reason").html(detail.RejectReason);
                    $("#messageModal_rejected_by").text(detail.RejectorId);
                    $("#messageModal_rejected_date").text(detail.RejectDate);
                },
                error: function (xhr, status, err) {
                    $("#messageModal_rejected_reason").text("error");
                    $("#messageModal_rejected_by").text("error");
                    $("#messageModal_rejected_date").text("error");
                }
            });
        }
        
        (function ($) {
            "use strict";

            $(document).ready(function () {

                $("#retrievable_claim_list button").click(function () {
                    $("#approval_form #actionButton").val("retrieve");
                    $("#approval_form #actionId").val(this.value);
                    $('#retrieveConfirmationModal').modal('show');
                });
 
                $("#retrieveConfirmationModal").click(function () {
                    $("#approval_form").submit();
                });

            });

        }(jQuery));
    </script>
}